import { QuestDefaultApi } from '@/library/util/api';
import { defaultErrorHandler } from '@/library/util/error-handler';
import { message } from 'ant-design-vue';
import { defineStore } from 'pinia';
import { useDesignContentStore } from '../design';
import { useDesignEditStore } from '../design/edit';
interface ManageDetailState {
  queId: string;

  isLoading: boolean;
}
export const useManageDetailStore = defineStore({
  id: 'ManageDetailStore',
  state: (): ManageDetailState => ({
    queId: '',
    isLoading: false,
  }),
  actions: {
    /** 设置当前操作id */
    setQuestId(value: ManageDetailState['queId']) {
      this.queId = value;
    },

    checkId(value: string) {
      if (!value) {
        throw new Error('问卷信息有误，请刷新重试');
      }
    },

    /** 发布 */
    async queryPublic(queId: string) {
      try {
        this.checkId(queId);
        this.isLoading = true;
        await QuestDefaultApi.questionaireReleaseGet({
          queId: queId,
        });
        message.success('发布成功');
      } catch (error) {
        defaultErrorHandler(error);
      } finally {
        this.isLoading = false;
      }
    },

    async querySuspect(queId: string) {
      try {
        this.checkId(queId);
        this.isLoading = true;
        await QuestDefaultApi.questionaireSuspendGet({
          queId: queId,
        });

        message.success('暂停成功');
      } catch (error) {
        defaultErrorHandler(error);
      } finally {
        this.isLoading = false;
      }
    },

    /**
     * 删除问卷
     * @param queId
     */
    async queryDelete(queId: string) {
      try {
        this.checkId(queId);
        this.isLoading = true;

        await QuestDefaultApi.questionaireDeleteQuestionaireGet({
          queId: queId,
        });
        message.success('删除成功');
      } catch (error) {
        defaultErrorHandler(error);
      } finally {
        this.isLoading = false;
      }
    },

    /**
     * 复制
     * @param queId
     */
    async queryCopy(queId: string) {
      try {
        this.isLoading = true;

        const { setQuestId, queryDetails } = useDesignEditStore();

        const {
          queryCreate,
          // getContentList,
          // getQuestionaire,
          // setQuestionaire,
        } = useDesignContentStore();

        setQuestId(queId);

        await queryDetails();

        await queryCreate();

        message.success('复制成功');
      } catch (error) {
        defaultErrorHandler(error);
      } finally {
        this.isLoading = false;
      }
    },
  },
});
